package com.yeban.javase.day12.test.streamtest;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/10/22
 */
public class CollectorsDemo {

    public static void main(String[] args) {

        //1.将英文字符串的数组中的元素全部变为大写，以集合输出。将整数的数组中的元素全部加3，以集合输出。
        String[] strs = {"Aba" , "Hello" , "World"};
        System.out.println("---------- 英文字符串数组全部转大写，以集合输出 ----------");
        List<String> strs1 = Arrays.asList(strs).stream().map(String::toUpperCase).collect(Collectors.toList());
        strs1.forEach(System.out::println);

        Integer[] nums = {1,2,3,4,5};
        System.out.println("---------- 整数的数组中的元素全部加3，以集合输出 ----------");
        List<Integer> nums1 = Arrays.stream(nums).map(n -> n + 3).collect(Collectors.toList());
        nums1.forEach(System.out::println);

        //2.统计员工人数、平均工资、工资总额、最高工资、统计工资的所有信息。
        //counting() 、averagingDouble()、maxBy()、summingInt()、summarizingDouble()
        List<Employee5> employees = Arrays.asList (
                new Employee5 ("张三", 7800.0, "男", "北京"),
                new Employee5 ("李四", 9200.0, "女", "上海"),
                new Employee5 ("王五", 6500.0, "男", "广州"),
                new Employee5 ("赵六", 10500.0, "女", "北京"),
                new Employee5 ("孙七", 8800.0, "男", "上海"),
                new Employee5 ("周八", 7200.0, "女", "广州"),
                new Employee5 ("吴九", 12000.0, "男", "北京"));

        System.out.println("---------- 统计人数 ----------");
        Long employeeNums = employees.stream().collect(Collectors.counting());
        System.out.println(employeeNums);

        System.out.println("---------- 平均工资 ----------");
        Double aveSalary = employees.stream().collect(Collectors.averagingDouble(Employee5::getSalary));
        System.out.println(aveSalary);

        System.out.println("---------- 工资总额 ----------");
        double sumSalary = employees.stream().collect(Collectors.summingDouble(Employee5::getSalary));
        System.out.println(sumSalary);

        System.out.println("---------- 最高工资 ----------");
        Double maxs = employees.stream().collect(Collectors.maxBy(Comparator.comparingDouble(Employee5::getSalary))).map(Employee5::getSalary).orElse(0.0);
        //Employee5 maxSalary = maxs.orElse(null);
        System.out.println(maxs);

        System.out.println("---------- 统计工资的所有信息 ----------");
        System.out.println(employees.stream().collect(Collectors.summarizingDouble(Employee5::getSalary)));


        // 3.将员工按薪资是否高于8000分为两部分；将员工按性别分组；将员工按性别和地区分组。
        // partitioningBy()、groupingBy()
        System.out.println("---------- 按薪资是否高于8000分组 ----------");
        Map<Boolean, List<Employee5>> salaryGroup = employees.stream().collect(Collectors.partitioningBy(e -> e.getSalary() > 8000));
        System.out.println("薪资高于8000的员工：" + salaryGroup.get(true));
        System.out.println("薪资低于8000的员工：" + salaryGroup.get(false));

        System.out.println("---------- 按性别分组 ----------");
        Map<String, List<Employee5>> genderGroup = employees.stream().collect(Collectors.groupingBy(Employee5::getGender));
        System.out.println("男性员工：" + genderGroup.get("男"));
        System.out.println("女性员工：" + genderGroup.get("女"));

        System.out.println("---------- 按性别和地区分组 ----------");
        Map<String, Map<String, List<Employee5>>> genderAndAreaGroup = employees.stream()
                .collect(Collectors.groupingBy(
                        Employee5::getGender,
                        Collectors.groupingBy(Employee5::getArea)
                ));
        System.out.println("男性各地区员工：" + genderAndAreaGroup.get("男"));
        System.out.println("女性各地区员工：" + genderAndAreaGroup.get("女"));

        // 4.将所有员工的名称用逗号拼接
        // joining()
        System.out.println("---------- 将所有员工的名称用逗号拼接 ----------");
        String nameTotals = employees.stream().map(Employee5::getName).collect(Collectors.joining(","));
        System.out.println(nameTotals);

    }
}

class Employee5 {
    private String name;
    private double salary;
    private String gender;
    private String area;

    public Employee5() {
    }

    public Employee5(String name, double salary, String gender, String area) {
        this.name = name;
        this.salary = salary;
        this.gender = gender;
        this.area = area;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return salary
     */
    public double getSalary() {
        return salary;
    }

    /**
     * 设置
     * @param salary
     */
    public void setSalary(double salary) {
        this.salary = salary;
    }

    /**
     * 获取
     * @return gender
     */
    public String getGender() {
        return gender;
    }

    /**
     * 设置
     * @param gender
     */
    public void setGender(String gender) {
        this.gender = gender;
    }

    /**
     * 获取
     * @return area
     */
    public String getArea() {
        return area;
    }

    /**
     * 设置
     * @param area
     */
    public void setArea(String area) {
        this.area = area;
    }

    public String toString() {
        return "Employee5{name = " + name + ", salary = " + salary + ", gender = " + gender + ", area = " + area + "}";
    }
}
